const express = require('express');
const router = express.Router();

const sequelize = require('../configs/sequelize_config');
const bizOrder = require('../models/order_model');

router.post('/create', async function(req, res, next) {
  try {
    const { body } = req;

    const result = await bizOrder.create({
      quality_code: body.quality_code,
      order_type: body.order_type,
      user_id: body.user_id
    });

    res.send({
      result
    });
  } catch (error) {
    res.send(error);
  }
});

router.get('/search', async function(req, res, next) {
  try {
    const result = await bizOrder.findAndCountAll({
      offset: 0,
      limit: 4
    });

    res.send({
      result
    });
  } catch (error) {
    res.send(error);
  }
});

router.get('/searchOrderAndItem', async function(req, res, next) {
  try {
    let queryStr =
        `select ord.order_id, ord.order_code, ord.order_type, ordItem.order_price, ordItem.order_quantity
            from biz_order as ord
            left join biz_order_item as ordItem
            on ordItem.order_id = ord.order_id`;
    const result = await sequelize.query(queryStr, {
      type: 'SELECT'
    });

    res.send({
      result
    });
  } catch (error) {
    res.send(error);
  }
});

module.exports = router;
